package com.kstudio.videocapture.listimage;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import com.kstudio.videocapture.R;
import com.kstudio.videocapture.util.KLog;

public class KImageListViewAlbumIcon extends View {
    private final KLog log = new KLog("KImageListViewAlbumIcon");

    private static final int BG_MAIN     = R.drawable.kimage_album_bg;

    private Context context;

    private Bitmap  BgMain;
    private Rect    BgMainBound;

    private Bitmap  Icon;
    private Rect    IconBound;

    private Paint IconPaint;
    private boolean LayoutChanged;

    public KImageListViewAlbumIcon(Context context) {
        this(context, null);
    }

    public KImageListViewAlbumIcon(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public KImageListViewAlbumIcon(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        this.context = context;
        init(attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        log.i("onMeasure width = %d , height = %d",MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec));
        int width = getMeasureSize(widthMeasureSpec, BgMain.getWidth());
        int height = getMeasureSize(heightMeasureSpec, BgMain.getHeight());

        setMeasuredDimension(width, height);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        if(changed || LayoutChanged) {
            int dstLeft = 0;
            int dstTop = 0;
            int dstRight = 0;
            int dstBottom = 0;

            dstLeft = 0;
            dstTop = 0;
            dstRight = BgMain.getWidth();
            dstBottom = BgMain.getHeight();

            BgMainBound.set(dstLeft, dstTop, dstRight, dstBottom);

            if(Icon != null) {
                dstLeft = (BgMain.getWidth() - Icon.getWidth()) / 2;
                dstTop = (BgMain.getHeight() - Icon.getHeight()) / 2;
                dstRight = dstLeft + Icon.getWidth();
                dstBottom = dstTop + Icon.getHeight();

                IconBound.set(dstLeft, dstTop, dstRight, dstBottom);
            }

            LayoutChanged = false;
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if(BgMain != null) {
            canvas.drawBitmap(BgMain, null, BgMainBound, IconPaint);
        }

        if(Icon != null) {
            canvas.drawBitmap(Icon, null, IconBound, IconPaint);
        }
    }

    public void setThumbnailBMP(Bitmap b) {
        if(Icon != null) {
            Icon.recycle();
            Icon = null;
            IconBound.set(0, 0, 0, 0);
        }

        if (b != null) {
            Icon = b.copy(Bitmap.Config.ARGB_8888,true);
            LayoutChanged = true;
            requestLayout();
        }
    }

    private void init(AttributeSet attrs, int defStyle) {
        BgMain = null;
        BgMainBound = new Rect();

        Icon = null;
        IconBound = new Rect();

        IconPaint = new Paint();
        LayoutChanged = true;

        if(attrs == null) {
            BgMain = BitmapFactory.decodeResource(context.getResources(), BG_MAIN);
        } else {
            int[] ids = new int[attrs.getAttributeCount()];
            for(int i = 0; i < attrs.getAttributeCount(); i++) {
                ids[i] = attrs.getAttributeNameResource(i);
            }

            TypedArray a = context.obtainStyledAttributes(attrs, ids, defStyle, 0);

            for(int i = 0; i < attrs.getAttributeCount(); i++) {
                String attrName = attrs.getAttributeName(i);
                if (attrName == null)
                    continue;

            }

            a.recycle();
        }
    }

    private int getMeasureSize(int measureSpec, int cSize) {
        int measureMode = MeasureSpec.getMode(measureSpec);
        int measureSize = MeasureSpec.getSize(measureSpec);
        int size = cSize;

        if(measureMode == MeasureSpec.EXACTLY){
            size = measureSize;
        } else if(measureMode == MeasureSpec.UNSPECIFIED || measureMode == MeasureSpec.AT_MOST){
            // Check against our minimum width
            size = Math.max(size, getSuggestedMinimumWidth());

            if (measureMode == MeasureSpec.AT_MOST) {
                size = Math.min(measureSize, size);
            }
        }

        return size;
    }

    public int getAlbumWidth() {
        return BgMain.getWidth();
    }

    public int getAlbumHeight() {
        return BgMain.getHeight();
    }
}
